//
//  ViewController.m
//  滑动隐藏状态栏和导航栏
//
//  Created by chenleping on 2018/6/13.
//  Copyright © 2018年 IAPTest. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()<UITableViewDelegate, UITableViewDataSource>
{
    CGFloat lastContentOffset;
    BOOL hiddenStatusBar;
}
@property (nonatomic, strong) UITableView * tableView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    self.title = @"---我是导航栏---";
    [self.view addSubview:self.tableView];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 40;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString * cellid = @"cellid";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellid];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellid];
    }
    cell.textLabel.text = @"我是中国人 ...";
    return cell;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    CGFloat newY= scrollView.contentOffset.y;
    NSLog(@"newY:===%f", newY);
    if (newY < 0) {
        hiddenStatusBar = NO;
        [self.navigationController setNavigationBarHidden:NO animated:YES];
    }else{
        if (newY != lastContentOffset ) {
            if (newY > lastContentOffset) { // scroll下滑...
                hiddenStatusBar = YES;
                [self.navigationController setNavigationBarHidden:YES animated:YES];
                
            }else{  // scroll上滑...
                hiddenStatusBar = NO;
                [self.navigationController setNavigationBarHidden:NO animated:YES];
                
            }
            lastContentOffset = newY;   // 记录上一次的偏移量
        }
    }
    
    [self setNeedsStatusBarAppearanceUpdate];   // 刷新状态栏的隐藏状态
}

- (BOOL)prefersStatusBarHidden{
    return hiddenStatusBar;
}

- (UITableView *)tableView{
    if (_tableView == nil) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.tableFooterView = [[UIView alloc] init];
    }
    return _tableView;
}

@end
